//
//  MyController.m
//  迷笛
//
//  Created by . on 2018/9/5.
//  Copyright © 2018年 🍃🌺🌺🍃. All rights reserved.
//

#import "MIDIMyController.h"
#import "MIDIMyViewCell.h"
#import "MIDImusicController.h"
#import "MIDIMymedalController.h"
#import "MIDIMycollectController.h"
#import "MIDIMyindentController.h"
#import "MIDIMycircleController.h"
#import "MIDIMyFocusController.h"
#import "MIDIactivityController.h"
#import "MIDIhairController.h"
@interface MIDIMyController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)UITableView * MyTable;
@property(nonatomic,strong)UIView * headvew;
@property(nonatomic,strong)UIImageView * headimage;//头像
@property(nonatomic,strong)UILabel * Neme;//昵称
@property(nonatomic,strong)UILabel * level;//等级
@property(nonatomic,strong)UILabel * ID;//用户id
@property(nonatomic,strong)UIButton * Sign;//签到
@property(nonatomic,strong)UILabel *nice1;
@property(nonatomic,strong)UILabel * balck;
@property(nonatomic,strong)NSArray * iconArr;
@property(nonatomic,strong)NSArray * tileArr;
@property(nonatomic,strong)UIImageView *imagev;
@property(nonatomic,strong)UIButton * backbtn;
@property(nonatomic,strong)NSString * apptoken;

@property(nonatomic,strong)UIView * notLogged;
@property(nonatomic,strong)UILabel * logintext;
@property(nonatomic,strong)UIImageView * heardLogin;
@property(nonatomic,strong)UILabel * logText;
@property(nonatomic,strong)UIButton * logBtn;
@property(nonatomic,copy)NSString * usrsapi;
@property(nonatomic,copy)NSDictionary * singDic;
@property(nonatomic,copy)NSDictionary * codeDic;

@property(nonatomic,copy)NSString * IDname;

@property(nonatomic,copy)NSString * signinapi;
@property(nonatomic,copy)NSString * singStr;

@property(nonatomic,copy)NSString * lootapi;

@end

@implementation MIDIMyController

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    UIBarButtonItem *leftBarItem = [[UIBarButtonItem alloc] initWithTitle:nil style:UIBarButtonItemStylePlain target:self action:@selector(Block:)];
//    self.navigationItem.leftBarButtonItem = leftBarItem;
//
//    [self.navigationItem.leftBarButtonItem setImage:[[UIImage imageNamed:@"后退"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    
    //   黑色字变为白色
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    _apptoken = [user objectForKey:@"APPToken"];

//    [self users];
    
    [self HomeUI];


    _iconArr  = @[@"矩形 4",@"形状 4",@"我的订单",@"消息",@"形状 5",@"wode-pinglun ",@"wode-richeng",@""];
    _tileArr = @[@"我的徽章",@"我的收藏",@"我的订单",@"我的评论",@"我的圈子",@"我的关注",@"活动日程",@"退出登陆"];

}
-(void)signin
{
    NSString * sing  =@"users/signin";
   
    
    _signinapi = [NSString stringWithFormat:@"%@%@",MIDIApiRoot,sing];

    NSDictionary * singdic = @{@"token":_apptoken};
    
    [EdHttp post:_signinapi params:singdic progress:^(id uploadProgress)
     {
         
     }
         success:^(id responseObject)
     {
         NSLog(@"签到接口%@",responseObject);
         self.singDic = [responseObject objectForKey:@"data"];

         if ([[responseObject objectForKey:@"code"] isEqualToNumber:@200])
         {
             [self btnUI];
             self.singStr = [self.singDic objectForKey:@"add_integral"];
             NSString * nicesing =[NSString stringWithFormat:@"获得%@积分",self.singStr];
             self.nice1.text = nicesing;
         }
         else
         {
             self.imagev.hidden = YES;
             [SVProgressHUD showSuccessWithStatus:@"今日已签到"];
             [SVProgressHUD dismissWithDelay:1.0];
         }
     }
         failure:^(NSError *error)
     {
         
     }];
    
}
-(void)sing:(UIButton *)sender
{
    [self signin];
}
-(void)users
{
    NSString * use  =@"users/info?token=";
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    _apptoken = [user objectForKey:@"APPToken"];
    _usrsapi = [NSString stringWithFormat:@"%@%@%@",MIDIApiRoot,use,_apptoken];
    
    [EdHttp get:_usrsapi params:nil progress:^(id downloadProgress)
     {
         
     }
        success:^(id responseObject)
     {
         NSLog(@"用户信息%@",responseObject);
         NSDictionary * dic = [responseObject objectForKey:@"data"];
         self.IDname = [dic objectForKey:@"name"];
         NSString * name =[NSString stringWithFormat:@"迷笛编号:%@",self.IDname];
         self.ID.text = name;
         NSLog(@"迷笛编号%@",self.IDname);

     }
        failure:^(NSError *error)
     {
         
     }];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
{
    return _iconArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    static NSString * cellID = @"cell";
    MIDIMyViewCell  * cell =[tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell)
    {
        cell =[[MIDIMyViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellID];
        cell.backgroundColor = [UIColor colorWithHexString:@"#1B212F"];
        [_MyTable setSeparatorColor : [UIColor blackColor]];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;

        
    }
    cell.icon.image = [UIImage imageNamed:_iconArr[indexPath.row]];
    cell.tile.text = _tileArr[indexPath.row];
    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    _apptoken = [user objectForKey:@"APPToken"];
    NSLog(@"我的Tableview-Token-%@",self.apptoken);
    if (self.apptoken == nil)
    {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"notification_modal_login" object:nil];
        return;
    }
    else
    {
//        ---
        if (indexPath.row == 0)
        {
            MIDIMymedalController * medal = [[MIDIMymedalController alloc]init];
            [self.navigationController pushViewController:medal animated:YES];
        }
        if (indexPath.row == 1)
        {
            MIDImusicController * collect = [[MIDImusicController alloc]init];
            [self.navigationController pushViewController:collect animated:YES];
        }
        if (indexPath.row == 2)
        {
            MIDIMyindentController * indent =[[MIDIMyindentController alloc]init];
            [self.navigationController pushViewController:indent animated:YES];
        }
        if (indexPath.row == 3)
        {
            MIDIhairController * miss =[[MIDIhairController alloc]init];
            [self.navigationController pushViewController:miss animated:YES];
        }
        if (indexPath.row == 4)
        {
            MIDIMycircleController * cirle =[[MIDIMycircleController alloc]init];
            [self.navigationController pushViewController:cirle animated:YES];
        }
        if (indexPath.row == 5)
        {
            MIDIMyFocusController * Foucus =[[MIDIMyFocusController alloc]init];
            [self.navigationController pushViewController:Foucus animated:YES];
        }
        if (indexPath.row == 6)
        {
            MIDIactivityController * activ = [[MIDIactivityController alloc]init];
            [self.navigationController pushViewController:activ animated:YES];
        }
        if (indexPath.row == 7)
        {
            
            
            UIAlertController *controller=[UIAlertController alertControllerWithTitle:@"是否退出？" message:nil preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction *act1=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                
            }];
            [act1 setValue:[UIColor colorWithHexString:@"#171717"] forKey:@"titleTextColor"];
            
            UIAlertAction *act2=[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action)
            {
                [self logout];
           

            }];
            [act2 setValue:[UIColor colorWithHexString:@"#FF005D"] forKey:@"titleTextColor"];
            
            [controller addAction:act1];
            [controller addAction:act2];
            [self presentViewController:controller animated:YES completion:^{
            }];
            
            
        }
//        ---
    }
    
   

}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}
- (void)viewWillAppear:(BOOL)animated
{
    
    self.tabBarController.tabBar.hidden = NO;
    self.navigationController.navigationBar.translucent = NO;//Bar的模糊效果，默认为YES

    self.navigationController.navigationBar.barTintColor = [UIColor colorWithHexString:@"#171717"];
    self.title = @"我的";
      [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor colorWithHexString:@"#FFFFFF"],NSFontAttributeName : [UIFont fontWithName:@"Helvetica-Bold" size:WidthScale(17)]}];
    //    去掉导航黑线
    UIImage *img = [UIImage imageNamed:@""];
    [self.navigationController.navigationBar setBackgroundImage:img  forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = [[UIImage alloc] init];
}
-(void)HomeUI
{
    [self.view addSubview:self.MyTable];
    [self.headvew addSubview:self.headimage];
    [self.headimage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.headvew);
        make.left.mas_equalTo(WidthScale(17));
        make.width.mas_equalTo(WidthScale(70));
        make.height.mas_equalTo(kDevice_Is_iPhoneX?HeighX(70):HeightScale(70));
    }];
    [self.headvew addSubview:self.Neme];
    [self.Neme mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(WidthScale(35));
        make.left.mas_equalTo(WidthScale(100));
        [self.Neme setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    }];
    [self.headvew addSubview:self.level];
    [self.level mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.Neme.mas_bottom).offset(WidthScale(5));
        make.left.equalTo(self.Neme);
        make.width.mas_equalTo(WidthScale(35));
        make.height.mas_equalTo(kDevice_Is_iPhoneX?HeighX(14):HeightScale(14));
//         [self.level setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    }];
    [self.headvew addSubview:self.ID];
    [self.ID mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.level.mas_bottom).offset(WidthScale(5));
        make.left.equalTo(self.Neme);
        [self.ID setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    }];
    [self.headvew addSubview:self.Sign];
    [self.Sign mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.headvew);
        make.right.mas_equalTo(WidthScale(-10));
        make.width.mas_equalTo(WidthScale(39));
        make.height.mas_equalTo(kDevice_Is_iPhoneX?HeighX(21):HeightScale(21));
        
    }];
    [self.headvew addSubview:self.balck];
    [self.balck mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.headimage.mas_bottom).offset(WidthScale(21));
        make.centerX.equalTo(self.headvew);
        make.width.equalTo(self.headvew);
        make.height.mas_equalTo(kDevice_Is_iPhoneX?HeighX(10):HeightScale(10));
    }];

}
-(UITableView *)MyTable
{
    if (!_MyTable)
    {
        _MyTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, kDevice_Is_iPhoneX?HeighX(700):HeightScale(600)) style:UITableViewStyleGrouped];
        _MyTable.dataSource = self;
        _MyTable.delegate = self;
        _MyTable.tableHeaderView = self.headvew;

        _MyTable.backgroundColor = [UIColor colorWithHexString:@"#171717"];
    }
    return _MyTable;
}
- (void)viewDidAppear:(BOOL)animated
 {
    [super viewDidAppear:animated];
    
     [self users];
     
//     NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
//     _apptoken = [user objectForKey:@"APPToken"];
//     NSLog(@"token=====---=-=-%@",self.apptoken);
     if (self.apptoken == nil)
     {
         _MyTable.tableHeaderView = self.notLogged;
     }
     else
     {

         _MyTable.tableHeaderView = self.headvew;
     }
}

-(UIView *)headvew
{
    if (!_headvew)
    {
        _headvew = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, kDevice_Is_iPhoneX?HeighX(134):HeightScale(134))];
        _headvew.backgroundColor =[UIColor colorWithHexString:@"#1B212F"];
    }
    return _headvew;
}
-(UIView *)notLogged
{
    if (!_notLogged)
    {
        _notLogged = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, kDevice_Is_iPhoneX?HeighX(134):HeightScale(134))];
        _notLogged.backgroundColor =[UIColor colorWithHexString:@"#1B212F"];
        [self notLoggedUI];

    }
    return _notLogged;
}
-(void)notLoggedUI
{
    [self.notLogged addSubview:self.heardLogin];
    [self.heardLogin mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.notLogged);
        make.left.mas_offset(WidthScale(20));
        make.width.mas_offset(70);
        make.height.mas_offset(70);
    }];
    [self.notLogged addSubview:self.logText];
    [self.logText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.heardLogin).mas_offset(WidthScale(13));
        make.left.equalTo(self.heardLogin.mas_right).mas_offset(WidthScale(10));
        [self.logText setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    }];
    [self.notLogged addSubview:self.logintext];
    [self.logintext mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.logText.mas_bottom).mas_offset(WidthScale(10));
        make.left.equalTo(self.logText);
        [self.logintext setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    }];
    [self.notLogged addSubview:self.logBtn];
    [self.logBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(WidthScale(0));
        make.left.mas_offset(WidthScale(0));
        make.width.mas_offset(WidthScale(270));
        make.height.mas_offset((kDevice_Is_iPhoneX?HeighX(134):HeightScale(134)));

    }];
}
-(UIImageView *)heardLogin
{
    if (!_heardLogin)
    {
        _heardLogin =[[UIImageView alloc]init];
        _heardLogin.image =[UIImage imageNamed:@"head_icon_nor"];
    }
    return _heardLogin;
}
-(UILabel *)logText
{
    if (!_logText)
    {
        _logText =[[UILabel alloc]init];
        _logText.text = @"点击登陆";
        _logText.font =[UIFont systemFontOfSize:WidthScale(18)];
        _logText.textColor = [UIColor whiteColor];
    }
    return _logText;
}
-(UILabel *)logintext
{
    if (!_logintext)
    {
        _logintext =[[UILabel alloc]init];
        _logintext.text = @"一秒登陆  体验更多功能";
        _logintext.font =[UIFont systemFontOfSize:WidthScale(13)];
        _logintext.textColor = [UIColor colorWithHexString:@"#00DFFF"];
    }
    return _logintext;
}
-(UIButton *)logBtn
{
    if (!_logBtn)
    {
        _logBtn =[[UIButton alloc]init];
        [self.logBtn addTarget:self action:@selector(Logbtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _logBtn;
}
-(void)Logbtn
{
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    _apptoken = [user objectForKey:@"APPToken"];
    
    NSLog(@"token=====---=-=-%@",self.apptoken);
    
    if (self.apptoken == nil)
    {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"notification_modal_login" object:nil];
        
        return;
    }
    else
    {
        _MyTable.tableHeaderView = self.headvew;
    }
}
-(UIImageView *)headimage//头像
{
    if (!_headimage)
    {
        _headimage =[[UIImageView alloc]init];
        _headimage.image = [UIImage imageNamed:@"IMG_0195.jpg"];
        _headimage.layer.masksToBounds = YES;

        [_headimage.layer setCornerRadius:WidthScale(35)]; //设置矩圆角半径
    }
    return _headimage;
}
-(UILabel *)Neme//昵称
{
    if (!_Neme)
    {
        _Neme =[[UILabel alloc]init];
        _Neme.text = @"我的名字";
        _Neme.textColor = [UIColor whiteColor];
        _Neme.font = [UIFont systemFontOfSize:WidthScale(20)];
    }
    return _Neme;
}
-(UILabel *)level
{
    if (!_level)
    {
        _level =[[UILabel alloc]init];
        _level.backgroundColor =[UIColor whiteColor];
        _level.textColor = [UIColor colorWithHexString:@"#01D6F5"];
        _level.text = @"Lv:1";
        _level.textAlignment = NSTextAlignmentCenter;
        _level.font =[UIFont systemFontOfSize:WidthScale(10)];
        _level.layer.masksToBounds = YES;
        [_level.layer setCornerRadius:WidthScale(5)]; //设置矩圆角半径
    }
    return _level;
}
-(UILabel *)ID
{
    if (!_ID)
    {
        _ID = [[UILabel alloc]init];
        NSString * name =[NSString stringWithFormat:@"迷笛编号%@",self.IDname];
        _ID.text = name;
        _ID.font = [UIFont systemFontOfSize:WidthScale(12)];
        _ID.textColor = [UIColor colorWithHexString:@"#00DFFF"];
    }
    return _ID;
}
-(UIButton *)Sign
{
    if (!_Sign)
    {
        _Sign = [[UIButton alloc]init];
        [_Sign setTitle:@"签到" forState:UIControlStateNormal];
        _Sign.titleLabel.font = [UIFont systemFontOfSize:WidthScale(10)];
        _Sign.layer.cornerRadius = 10;
        //将多余的部分切掉
        _Sign.layer.masksToBounds = YES;
        [_Sign.layer setBorderColor:[UIColor redColor].CGColor];
        [_Sign.layer setBorderWidth:1];
        [_Sign.layer setMasksToBounds:YES];
        [_Sign addTarget:self action:@selector(sing:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _Sign;
}
-(UILabel *)balck
{
    if (!_balck)
    {
        _balck = [[UILabel alloc]init];
        _balck.backgroundColor = [UIColor blackColor];
    }
    return _balck;
}
-(void)btnUI
{
    _backbtn =[[UIButton alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [_backbtn addTarget:self action:@selector(backbrn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_backbtn];
    _imagev =[[UIImageView alloc]init];
    _imagev.image =[UIImage imageNamed:@"qiandao-beijing"];
    [self.view addSubview:_imagev];
    [_imagev mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.view);
        make.centerX.equalTo(self.view);
        make.width.mas_offset(WidthScale(200));
        make.height.mas_offset(kDevice_Is_iPhoneX?HeighX(160):HeightScale(160));
    }];
    UILabel * nice =[[UILabel alloc]init];
    nice.text = @"签到成功";
    nice.textColor =[UIColor colorWithHexString:@"#FFFFFF"];
    nice.font =[UIFont systemFontOfSize:WidthScale(20)];
    [_imagev addSubview:nice];
    [nice mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.imagev).offset(WidthScale(80));
        make.centerX.equalTo(self.imagev);
        [nice setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    }];
    self.nice1 =[[UILabel alloc]init];
    //    self.nice1.text = @"获得10积分";
    //        NSString * nicesing =[NSString stringWithFormat:@"获得%@积分",self.singStr];
    //        self.nice1.text = nicesing;
    
    self.nice1.textColor =[UIColor colorWithHexString:@"#FFFFFF"];
    self.nice1.font =[UIFont systemFontOfSize:WidthScale(16)];
    [_imagev addSubview:self.nice1];
    [self.nice1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(nice.mas_bottom).offset(WidthScale(10));
        make.centerX.equalTo(self.imagev);
        [self.nice1 setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    }];
}
-(void)backbrn:(UIButton * )sender
{
    _imagev.hidden = YES;
    _backbtn.hidden = YES;
}

//// 返回上一级
//-(void)Block:(UIButton * )sender
//{
//    [self.navigationController popViewControllerAnimated:YES];
//}

-(void)logout
{
    NSString * logstr = @"logout";
    self.lootapi = [NSString stringWithFormat:@"%@%@?token=%@",MIDIApiRoot,logstr,_apptoken];
    
    [EdHttp get:self.lootapi params:nil progress:^(id downloadProgress) {
        
    } success:^(id responseObject)
     {
         NSLog(@"退出成功%@",responseObject);
         NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
         [defaults removeObjectForKey:@"APPToken"];
         [defaults synchronize];//快速保存
         [SVProgressHUD showErrorWithStatus:@"退出成功"];
         [SVProgressHUD dismissWithDelay:1.0];
         self.MyTable.tableHeaderView = self.notLogged;
         
    } failure:^(NSError *error) {
        NSLog(@"失败");
    }];
    
}

@end
